package com.ksvip.next.components.datasecure.fieldhandlers;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ksvip.next.components.datasecure.common.annotations.SensitiveJson;
import com.ksvip.next.components.datasecure.common.enums.SensitiveRulesEnum;
import com.ksvip.next.components.datasecure.utils.SensitiveProcessUtils;

/**
 * json字段脱敏处理器
 *
 * @author jiangchenxi on 2017/6/21.
 */
public class JsonFieldHandler implements FieldHandler {
    @Override
    public boolean ignore(Field field) {
        SensitiveJson sensitiveJson = field.getAnnotation(SensitiveJson.class);
        if (sensitiveJson == null) {
            return false;
        }
        if (sensitiveJson.ignore()) {
            return true;
        }
        return false;
    }

    @Override
    public Object getValue(Field field, Object fieldValue) {
        SensitiveJson sensitiveJson = field.getAnnotation(SensitiveJson.class);
        if (sensitiveJson == null || fieldValue == null) {
            return fieldValue;
        }

        String formatPattern = sensitiveJson.format();
        JSONObject formatPatternJson = null;
        formatPatternJson = JSONObject.parseObject(formatPattern);
        if (MapUtils.isEmpty(formatPatternJson)) {
            return fieldValue;
        }

        String jsonVal = StringUtils.EMPTY;
        if (fieldValue instanceof String) {
            jsonVal = String.valueOf(fieldValue);
        } else {
            String format = "millis";
//            jsonVal = JSON.toJSONString(fieldValue, SerializerFeature.WriteDateUseDateFormat);
          jsonVal = JSON.toJSONString(fieldValue, format);

        }

        Map<String, SensitiveRulesEnum> fieldsRuleMap = new HashMap<String, SensitiveRulesEnum>();
        for (Map.Entry<String, Object> entry : formatPatternJson.entrySet()) {
            String key = entry.getKey();
            String singleFormatPattern = String.valueOf(formatPatternJson.get(key));
            SensitiveRulesEnum sensitiveRulesEnum = SensitiveRulesEnum.getSensitiveRule(singleFormatPattern);
            if (sensitiveRulesEnum == null) {
                continue;
            }
            fieldsRuleMap.put(key, sensitiveRulesEnum);
        }
        return SensitiveProcessUtils.jsonShield(jsonVal, fieldsRuleMap);
    }
}
